package org.wlgzs.shop.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import groovy.util.logging.Slf4j;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.wlgzs.shop.bean.GoodsData;
import org.wlgzs.shop.bean.Msg;
import org.wlgzs.shop.bean.ShowDate;
import org.wlgzs.shop.service.IGoodsDateService;
import org.wlgzs.shop.service.IStoreService;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;


@Api(tags = "这是商品的接口", value = "GoodsController")
@Controller
@RequestMapping("/goods")
@Slf4j
public class GoodsController {
    @Resource( name = "GoodsDateService")
    IGoodsDateService goodsDateService;

    @Resource(name = "StroeService")
    IStoreService storeService;

    /**
     *
     * @param goodsData
     * @param twoKind
     * @return Msg
     *
     * 添加商品
     */
    @RequestMapping("/addGoods")
    @ResponseBody
    @ApiOperation(value = "添加商品",notes = "通过获取信息添加商品",httpMethod = "POST", response =Msg.class)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "goodsData",value = "添加商品类",required = true,dataType = "GoodsData"),
            @ApiImplicitParam(paramType = "query",name = "twoKind",value = "二级分类",required = true,dataType = "String"),
    })
    public Msg addGoods(@Valid GoodsData goodsData, @Param("twoKind")String twoKind,BindingResult result){
        if(result.hasErrors()){
            return Msg.fail().add("errors","商品信息不完整！");
        }
        else {
            return goodsDateService.addGood(goodsData, twoKind);
        }
    }


    /**
     * @param goodsId
     * @return Msg
     *
     *删除商品
     */
    @RequestMapping("/deleteGoods")
    @ResponseBody
    @ApiOperation(value = "删除商品",notes = "根据商品id删除商品",httpMethod = "POST", response =Msg.class)
    @ApiImplicitParam(paramType = "query",name = "goodsId",value = "商品id",required = true,dataType = "int")
    public Msg deleteGoods(int goodsId){
        return  goodsDateService.deleteGoods(goodsId);
    }

    /**
     *
     * @param data
     * @return Msg
     *
     *修改商品价格
     */
    @ResponseBody
    @RequestMapping("/setGoodsPerice")
    @ApiOperation(value = "修改商品价格",notes = "根据商品的id修改上价格",httpMethod = "POST", response =Msg.class)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "商品id", required = true, dataType = "int"),
            @ApiImplicitParam(paramType = "query",name = "perice",value = "修改后的商品价格",required = true,dataType = "float")
    })
    public Msg setGoodsPerice(GoodsData data){
        return goodsDateService.setGoodPerice(data);
    }
    


    /**
     * 查询所有商品
     *
     *
     */
    @RequestMapping("allGoods")
    @ResponseBody
    @ApiOperation(value = "查询所有商品",httpMethod = "POST", response =PageInfo .class)
    @ApiImplicitParam(paramType = "query",name = "page",value = "页数,默认为 1",required = true,dataType = "int")
    public PageInfo allGoods(@RequestParam(value = "page", defaultValue = "1")Integer page){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("special/common");
        PageHelper.startPage(page,15);
        List<GoodsData> list=goodsDateService.allGoods();
        PageInfo pageInfo=new PageInfo(list,15);
        modelAndView.addObject("goods",pageInfo);
        return pageInfo;
    }

    /**
     * 根据id，查询单个商品
     *
     */
    @RequestMapping("/selectOneGood")
    @ApiOperation(value = "查询单个商品",httpMethod = "POST",response = String.class)
    @ApiImplicitParam(paramType = "query",name = "goodId",value = "商品id",required = true,dataType = "int")
    public String selectGood(int goodId, Model model){
        GoodsData data=goodsDateService.selectGoods(goodId);
        String photoName=data.getPhotoUrl();
        String[] arr=photoName.split(",");
        List<ShowDate> list=goodsDateService.selectGoodsShow(goodsDateService.selectDate(data.getGoodsKind()),6);
        model.addAttribute("likeDate1",list.subList(0,3));
        model.addAttribute("likeDate2",list.subList(3,6));
        model.addAttribute("photoName",arr);
        model.addAttribute("goods",data);
        return "goods/goods";
    }


    //根据二级分类查询商品信息
    @RequestMapping("/selectGoodsByKind")
    @ApiOperation(value = "根据二级分类查询商品信息",httpMethod = "POST",response = ModelAndView.class)
    public  ModelAndView seletGoodsByKind(@Param(" kindOne") String kindTwo){
        ModelAndView modelAndView=new ModelAndView();
        goodsDateService.selectDate(kindTwo);
        return modelAndView;
    }


@ApiOperation(value = "跳转'cashier'页面",httpMethod = "GET",response = String.class)
    @RequestMapping("cashier")
    public String cashierJump(){
        return "goods/cashier";
}


    @ApiOperation(value = "跳转'goods'页面",httpMethod = "GET",response = String.class)
    @RequestMapping("/goods")
    public String goodsJump(){
        return "goods/goods";
    }


    @ApiOperation(value = "跳转'special'页面",httpMethod = "GET",response = String.class)
    @RequestMapping("/special")
    public String commonJump(int storeId){
        //分类
        List<String> listKind=goodsDateService.selectKindByStore(storeId);
        //商品
        List<ShowDate> list=goodsDateService.selectByStore(storeId);
        //店铺信息
        return "special/common";
    }


}
